package com.sinodata.bsm.cicp.finder.aix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * Description: AIX网络接口发现程序
 * 程序返回：物理接口名
 */

public class Net extends AixFinder {
    //注册Log方法
    private static final Logger logger = Logger.getLogger(Net.class);
    private static final String SHALL = "netstat -i";

    public Net() {
    }

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        Set<String> portName = new HashSet<String>();
        String[] netRes = null;
        BufferedReader reader = null;
        String strLine = null;
        StringTokenizer st = null;
        try {
            reader = CommandReader.getReader(ip, SHALL, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                if (strLine.length() > 0) {
                    st = new StringTokenizer(strLine);
                    String name = st.nextToken();
                    if (!portName.contains(name) && !"name".equalsIgnoreCase(name)) {
                        netRes = new String[4];
                        list.add(netRes);
                        netRes[0] = name;
                        netRes[1] = name;
                        netRes[2] = getHostName(ip) + " Net Port " + name;
                        netRes[3] = name; //物理接口名
                        portName.add(name);
                    }
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find aix Net Port", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
